﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;
using Utility;
using System.Configuration;

public partial class Admin_DonHang : System.Web.UI.Page
{
    protected string CurrentColumnSort
    {
        get
        {
            return ViewState["CurrentColumnSort"].ToString();
        }
        set
        {
            ViewState["CurrentColumnSort"] = value;
        }
    }
    protected int STT
    {
        get
        {
            return int.Parse(ViewState["STT"].ToString());
        }
        set
        {
            ViewState["STT"] = value;
        }
    }
    protected string OrderType
    {
        get
        {
            return ViewState["OrderType"].ToString();
        }
        set
        {
            ViewState["OrderType"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.CurrentColumnSort = HoaDonBanHangColumn.MaHoaDonBanHang.ToString();
            this.OrderType = OrderDirection.DESC.ToString();
            LoadData();
        }
    }
    private void LoadData()
    {
        try
        {
            if (int.Parse(Session["MaPhanQuyen"].ToString()) == 0 || int.Parse(Session["MaPhanQuyen"].ToString()) == 1)
            {
                btn_CapNhatTrangThai.Visible = true;

            }
            else
            {
                btn_CapNhatTrangThai.Visible = false;
            }
            STT = (pager.CurrentIndex - 1) * pager.PageSize + 1;
            int total = 0;
            pager.PageSize = 1;// int.Parse(ConfigurationManager.AppSettings["pagesize"]);

            HoaDonBanHangCollection DHCollection = HoaDonBanHangBUS.GET("where 1=1",pager.CurrentIndex, pager.PageSize, this.CurrentColumnSort,
                this.OrderType, out total);
            pager.ItemCount = total;
            rpt_DonHang.DataSource = DHCollection;
            rpt_DonHang.DataBind();
            if (pager.ItemCount <= 0 || pager.ItemCount <= pager.PageSize)
                pager.Visible = false;
            else
                pager.Visible = true;
        }
        catch
        {
            
        }
    }

    protected void rpt_DonHang_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem
            || e.Item.ItemType == ListItemType.Item)
        {
            HoaDonBanHang DH = (HoaDonBanHang)e.Item.DataItem;

            Label lb_STT = (Label)e.Item.FindControl("lb_STT");
            lb_STT.Text = Convert.ToString(STT + e.Item.ItemIndex);

            HiddenField hf_MaDonHang = (HiddenField)e.Item.FindControl("hf_MaDonHang");
            hf_MaDonHang.Value = DH.MaHoaDonBanHang.ToString();

            HyperLink hl_XemChiTiet = (HyperLink)e.Item.FindControl("hl_XemChiTiet");
            hl_XemChiTiet.NavigateUrl = "ChiTietDonHang.aspx?id=" + DH.MaHoaDonBanHang.ToString();

            DropDownList dropdownlist_TrangThai = (DropDownList)e.Item.FindControl("dropdownlist_TrangThai");
            dropdownlist_TrangThai.Items.Insert(0, "None");
            dropdownlist_TrangThai.Items.Insert(1, "Vừa giao hàng");
            dropdownlist_TrangThai.Items.Insert(2, "Đã thanh toán");
            dropdownlist_TrangThai.Items.Insert(3, "Đã giao hàng");
            dropdownlist_TrangThai.Items.Insert(4, "Hoàn thành");
            dropdownlist_TrangThai.SelectedIndex = DH.MaTinhTrangHoaDon;
        }
    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }
    protected void btn_CapNhatTrangThai_Click(object sender, EventArgs e)
    {
        
        for (int i = 0; i < rpt_DonHang.Items.Count; i++)
        {
            RepeaterItem item = rpt_DonHang.Items[i];
            DropDownList dropdownlist_TrangThai = (DropDownList)item.FindControl("dropdownlist_TrangThai");
            HiddenField hf_MaDonHang = (HiddenField)item.FindControl("hf_MaDonHang");
            HoaDonBanHangBUS.UPDATE_TrangThai(int.Parse(hf_MaDonHang.Value), dropdownlist_TrangThai.SelectedIndex);
        }
        LoadData();
    }
}
